<?php

namespace App\Helpers;

use App\Models\SubAccount\SubAccountStaffBind;

trait SubAccountTrait
{

    public function getAuthSubIds($user){
        $ids=[];
        if($user){
            return SubAccountStaffBind::where('staff_id',$user->id)->pluck('sub_account_id');
        }
        return $ids;
    }

    //早班晚班筛选
    public function setShiftWhere($user,$loginLogoutRecord){
        $today=date('Y-m-d');
        $shiftTime=$this->getShiftTime($user);
        //早班下班时间与晚班上班时间相同
        if($shiftTime['day_off']==$shiftTime['night_on']){
            if(time()<=strtotime($today.' '.$shiftTime['day_off'])){
                $shiftName='早班';
            }else{
                $shiftName='晚班';
            }
            return $loginLogoutRecord->where('shift_name',$shiftName);
        }
        //时间有重叠
        if(time()<=strtotime($today.' '.$shiftTime['night_on'])){
            return $loginLogoutRecord->where('shift_name','早班');
        }else if(time()>strtotime($today.' '.$shiftTime['day_off'])){
            return $loginLogoutRecord->where('shift_name','晚班');
        }else{
            return $loginLogoutRecord;
        }
    }
    //获取上下班时间
    public function getShiftTime($user){
        $time=[
            'day_off'=>'16:00',
            'night_on'=>'16:00',
        ];
        if($user->two && $user->two->shift){
            $time=[
                'day_off'=>$user->two->shift->day_shift_end??'16:00',
                'night_on'=>$user->two->shift->night_shift_start??'16:00',
            ];
        }
        return $time;
    }

}
